<template>
  <PanleHead :route="route"></PanleHead>
  <div class="btns">
    <el-button @click="open(null)" type="primary" icon="Plus" size="small"
      >新增</el-button
    >
  </div>
  <el-table :data="tableData.list" style="width: 100%">
    <el-table-column prop="id" label="id"> </el-table-column>
    <el-table-column prop="name" label="昵称"> </el-table-column>
    <el-table-column prop="permissionName" label="菜单权限" width="500">
    </el-table-column>
    <el-table-column label="操作">
      <template #default="scope">
        <div>
          <el-button type="primary" @click="open(scope.row)">编辑</el-button>
        </div>
      </template>
    </el-table-column>
  </el-table>
  <div class="pagination-info">
    <el-pagination
      v-model:current-page="paginationData.pageNum"
      :page-size="paginationData.pageSize"
      :background="false"
      layout="total, prev, pager, next"
      :total="tableData.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      size="small"
    />
  </div>
  <el-dialog
    v-model="dialogFromVisiable"
    :before-close="beforeClose"
    title="添加权限"
    width="500"
  >
    <el-form
      ref="formRef"
      label-width="100px"
      label-position="left"
      :model="form"
      :rules="rules"
    >
      <el-form-item prop="id" v-show="false">
        <el-input v-model="form.id"></el-input>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="form.name" placeholder="请填写权限名称"></el-input>
      </el-form-item>
      <el-form-item label="权限" prop="permissions">
        <el-tree
          :data="permissionData"
          style="max-width: 600px"
          node-key="id"
          :show-checkbox="true"
          :default-checked-keys="defaultKeys"
          ref="treeRef"
          :default-expanded-keys="[2]"
        >
        </el-tree>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="confirm(formRef)">确认</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { getMenuList, userGetmenu, userSetMune } from "@/api";
import PanleHead from "@/components/PanleHead.vue";
import { nextTick, onMounted, ref } from "vue";
import { useRoute } from "vue-router";
const form = ref({
  name: "",
  permissions: "",
  id: "",
});
const route = useRoute();
const dialogFromVisiable = ref(false);
const beforeClose = () => {
  dialogFromVisiable.value = false;
  // 重置表单数据
  formRef.value.resetFields();
  treeRef.value.setCheckedKeys(defaultKeys);
};
// 树形菜单权限数据
const permissionData = ref([]);
onMounted(() => {
  getuserGetmenu();
  getLisData();
});
const getuserGetmenu = async () => {
  const res = await userGetmenu();
  console.log(res);
  permissionData.value = res.data.data;
};
//选中权限
const defaultKeys = [4, 5];
const treeRef = ref();
const formRef = ref();
// 表单提交
const confirm = async (formEl) => {
  if (!formEl) return;
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      const permissions = JSON.stringify(treeRef.value.getCheckedKeys());
      console.log(permissions);
      const res = await userSetMune({
        name: form.value.name,
        permissions,
        id: form.value.id,
      });
      console.log(res);
      beforeClose();
      getLisData();
    } else {
      console.log("erro submit", fields);
    }
  });
};
const rules = ref({
  name: [
    {
      required: true,
      trigger: "blur",
      message: "请输入权限名称",
    },
  ],
});
const paginationData = ref({
  pageNum: 1,
  pageSize: 2,
});
const handleSizeChange = (value) => {
  paginationData.value.pageSize = value;
  getLisData();
};
const handleCurrentChange = (value) => {
  paginationData.value.pageNum = value;
  getLisData();
};
// 请求列表数据
const getLisData = async () => {
  const res = await getMenuList(paginationData.value);
  console.log(res);
  const { list, total } = res.data.data;
  tableData.value.list = list;
  tableData.value.total = total;
  console.log(tableData);
};
// 打开弹窗
const open = (rowData) => {
  dialogFromVisiable.value = true;
  //弹窗打开form生成是异步
  nextTick(() => {
    if (rowData) {
      console.log(222);
      console.log(rowData);

      Object.assign(form.value, { id: rowData.id, name: rowData.name });
      treeRef.value.setCheckedKeys(rowData.permission);
    }
  });
};
// 列表数据
const tableData = ref({
  list: [],
  total: 0,
});
console.log(tableData.value);
</script>

<style scoped></style>
